package com.k.comnote.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.k.comnote.R;
import com.k.comnote.model.ItemNoteRecent;

import java.util.List;

/**
 * Created by K on 2015/8/20.
 */
public class MyAdapterForRecent extends BaseAdapter {

    private List<ItemNoteRecent> list;
    private LayoutInflater mInflater;


    public MyAdapterForRecent(Context context, List<ItemNoteRecent> list) {
        this.list = list;
        mInflater = LayoutInflater.from(context);
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.itemrecently, null);
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            convertView.setLayoutParams(lp);
            viewHolder.title = (TextView) convertView.findViewById(R.id.recently_title);
            viewHolder.time = (TextView) convertView.findViewById(R.id.recently_time);
            viewHolder.content = (TextView) convertView.findViewById(R.id.recently_content);
            viewHolder.image = (ImageView) convertView.findViewById(R.id.recently_image);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        ItemNoteRecent item = list.get(position);
        viewHolder.title.setText(item.title);
        viewHolder.time.setText(item.time);
        viewHolder.content.setText(item.content);
        viewHolder.image.setImageResource(item.imageRes);

        return convertView;
    }

    class ViewHolder {
        private TextView title;
        private TextView time;
        private TextView content;
        private ImageView image;
    }

}
